package creational.pattern.builder;

import lombok.Data;
import org.springframework.util.StringUtils;

/**
 * @Autor: yuanyongan
 * @Description: 建造者模式下实现连接池的配置类
 * @Date 2023/1/9 10:04
 */
@Data
public class ResourcePoolConfigBuilder {
    private String name;
    private int maxTotal;
    private int maxIdle;
    private int minIdle;
    private ResourcePoolConfigBuilder(Builder builder){
        this.name = builder.name;
        this.maxIdle = builder.maxIdle;
        this.maxTotal = builder.maxTotal;
        this.minIdle = builder.minIdle;
    }
    public static class Builder{
        private static final int DEFAULT_MAX_TOTAL = 8;
        private static final int DEFAULT_MAX_IDLE = 8;
        private static final int DEFAULT_MIN_IDLE = 0;
        private String name;

        private int maxTotal = DEFAULT_MAX_TOTAL;
        private int maxIdle = DEFAULT_MAX_IDLE;
        private int minIdle = DEFAULT_MIN_IDLE;

        public ResourcePoolConfigBuilder build(){
            if (StringUtils.isEmpty(name)){
                throw new IllegalArgumentException("...");
            }
            if (maxIdle > maxTotal){
                throw new IllegalArgumentException("...");
            }
            if (minIdle > maxIdle || minIdle > maxTotal){
                throw new IllegalArgumentException("...");
            }
            return new ResourcePoolConfigBuilder(this);
        }
        public Builder setName(String name){
            this.name = name;
            return this;
        }
        public Builder setMaxTotal(int maxTotal){
            this.maxTotal = maxTotal;
            return this;
        }
        public Builder setMaxIdle(int maxIdle){
            this.maxIdle = maxIdle;
            return this;
        }
        public Builder setMinIdle(int minIdle){
            this.minIdle = minIdle;
            return this;
        }
    }
}
